package uw10.app.display;

import uw10.app.data.Event;
import uw10.app.data.GraphBounds;
import uw10.app.data.GraphData;
import uw10.app.data.GraphIndicator;
import uw10.app.data.sources.EventGraphDataSource;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.Bitmap.Config;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

public class EventRaterView extends View implements OnTouchListener
{
	public Context context;
	
	private Event event;
	
	private EventRaterListener listener;
	
	private Bitmap gradient;
	
	private boolean ready;
	
	private int maxRating;
	
	private Bitmap rater;
	private float ratingPos;
	
	private boolean vertical;
	
	
	public EventRaterView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		this.context = context;
		setOnTouchListener(this);
	}
	
	public void setup(boolean vertical, int maxRating, RatingColorFactory factory, int detail, Bitmap rater)
	{
		this.vertical = vertical;
		this.maxRating = maxRating;
		this.rater = rater;
		
		float[] pos = new float[detail + 1];
		int[] color = new int[detail + 1];
		
		for (int i = 0; i <= detail; i++)
		{
			pos[i] = i / (float)detail;
			if (vertical)
				color[i] = factory.getColor((int)(maxRating * (1.0f - pos[i])));
			else
				color[i] = factory.getColor((int)(maxRating * pos[i]));
		}
		
		if (vertical)
			gradient = Bitmap.createBitmap(1, 64, Config.ARGB_8888);
		else
			gradient = Bitmap.createBitmap(64, 1, Config.ARGB_8888);
		new GradientRenderer().renderGradient(gradient, pos, color);
	}
	
	public void setListener(EventRaterListener listener)
	{
		this.listener = listener;
	}
	
	public synchronized void setEvent(Event event)
	{
		this.event = event;
		
		ratingPos = ((float)event.rating) / maxRating;
		
		ready = true;
		
		this.invalidate();
	}
	
	public synchronized void onDraw(Canvas c)
	{
		if (ready)
		{
			c.drawBitmap(gradient, null, c.getClipBounds(), null);
			
			Rect r = new Rect();
			c.getClipBounds(r);
			if (vertical)
			{
				int h = r.height();
				r.top += h * 0.9f * (1.0f - ratingPos);
				r.bottom = (int) (r.top + h * 0.1f);
			}
			else
			{
				int w = r.width();
				r.left += w * 0.9f * ratingPos;
				r.right = (int) (r.left + w * 0.1f);
			}
			
			c.drawBitmap(rater, null, r, null);
		}
	}

	public synchronized boolean onTouch(View arg0, MotionEvent e)
	{
		float newRating;
		
		if (vertical)
			newRating = 1.0f - (e.getY() / getHeight());
		else
			newRating = e.getX() / getWidth();
		
		if (newRating >= 0 && newRating < 1)
		{
			ratingPos = newRating;
			
			postInvalidate();
			
			if (e.getAction() == MotionEvent.ACTION_UP)
			{
				if (listener != null) listener.onEventRatingSelected(event, (int) (maxRating * ratingPos));
			}
			
			return true;
		}
		else return false;
	}
}
